import { _decorator, Component, Node,Sprite, tween, Tween, Vec3,Animation, CharacterController, animation, math, SkeletalAnimation, randomRange, randomRangeInt} from 'cc';
import { ResourceManager } from '../PlayableFramework/Tools/Manager/ResourceManager';
import { ProgressBar } from '../PlayableFramework/BusinessLogic/ProgressBar';
import { PoolObject } from '../PlayableFramework/Tools/Pool/PoolObject';
import { PathMove } from '../PlayableFramework/Tools/Path/PathMove';
import { PathConfig } from './PathConfig';
const { ccclass, property } = _decorator;

@ccclass('Actor')
export class Actor extends PoolObject {

    public roomIndex:number;
    public seatIndex:number;

    @property(SkeletalAnimation)
    public anim:SkeletalAnimation;

    @property(Node)
    public walkBike:Node

    @property(Node)
    public rideBike:Node

    public tween:Tween<Node>;

    public mover:PathMove;

    @property(Node)
    public emojiNode:Node;


    start() {

    }


    public override init(){
        this.mover = this.getComponent(PathMove);
        {
            this.mover.target = this.node;
            this.mover.events.push(()=>{
                this.mover.onNextPointChanged = (next)=>{
                    var dis = Vec3.distance(next,this.node.worldPosition);
                    if(dis > 0.01){
                        this.facePoint(next);
                    }
                };
            });
        }

        this.rideBike.active = false;
        this.walkBike.active = true;

        this.anim.play("ride_bike");
    }
    
    public override onSpawn() {
        this.seatIndex = -1;
        this.roomIndex = -1;
        this.hideEmoji();
        this.emojiNode.active = true;
        this.emojiNode.children[0].getChildByName(randomRangeInt(0,4).toString()).active = true;
        this.mover.movementParameter = 200;
    }

    public hideEmoji(){
        for (let i = 0; i < 4; i++) {
            this.emojiNode.children[0].getChildByName(i.toString()).active = false;
        }
        this.emojiNode.active = false;
    }

    public fixBike(func:()=>void)
    {
        var bar =  ResourceManager.spawn("FixBar").getComponent(ProgressBar);
        bar.node.setParent(ResourceManager.getPoolNode("转圈Parent"));
        var anim = bar.node.getComponent(Animation);
        anim.play("progress@show");
        bar.node.worldPosition = this.node.worldPosition.clone().add(new Vec3(0,100,0));
        bar.excute(1,-1,func);
    }

    public entryRoom(func:()=>void){
        var path = PathConfig.instance.getEntryToRoom(this.roomIndex);
        this.mover.setPathPoints(path);
        this.mover.onPathComplete = ()=>{
            func();
        };
        this.mover.move();
        this.anim.play("walk_bike");
    }
    
    public moveToRoom(func:()=>void,func2:(index : number)=>void){
        this.hideEmoji();
        var path = PathConfig.instance.getEntryRoad(this.roomIndex);
        this.mover.setPathRoot(path);
        this.mover.onPathComplete = ()=>{
            func();
        };
        this.mover.onPointComplete = func2;
        this.mover.move();
        this.anim.play("walk_bike");
    }

    public moveToRoomSeat(func:()=>void){
        var path = PathConfig.instance.getRoomEntryToSeat(this.roomIndex,this.seatIndex);
        this.mover.setPathPoints(path);
        this.mover.onPathComplete = ()=>{
            func();
        };
        this.mover.move();
        this.anim.play("walk_bike");
    }
    
    public moveToRoomExit(func:()=>void){
        var path = PathConfig.instance.getRoomSeatToExit(this.roomIndex,this.seatIndex);
        this.mover.setPathPoints(path);
        this.mover.onPathComplete = ()=>{
            func();
        };
        this.mover.move();
        this.anim.play("ride_bike");
    }

    public moveToExit(func:()=>void){
        var path = PathConfig.instance.getRoomToExit(this.roomIndex);
        this.mover.setPathPoints(path);
        this.mover.onPathComplete = ()=>{
            func();
        };
        this.mover.move();
        this.anim.play("ride_bike");
    }

    public dotween(func:(actor:Actor)=>Tween<Node>){
        if(this.tween!=null)
        {
            this.tween.stop();
            this.tween = null;
            this.anim.play("idle_bike");
        }
        this.tween = func(this);
    }

    public face(face:Vec3){
        var direction = face.normalize();
                
        var angle =  Vec3.signedAngle(Vec3.UP,direction,Vec3.FORWARD)* (180 / Math.PI);
        this.node.children[0].children[0].setRotationFromEuler(0, -angle, 0);
    }

    public facePoint(face:Vec3){
        let direction = face.clone().subtract(this.node.worldPosition);
        direction = direction.normalize();
                
        var angle =  Vec3.signedAngle(Vec3.UP,direction,Vec3.FORWARD)* (180 / Math.PI);
        this.node.children[0].children[0].setRotationFromEuler(0, -angle, 0);
    }
}


